var books = [];
var saving = false; //保存中

loadData();

function loadData(data){
    loading.style.display = "block";
    ajax("find_all_book",data,function(datas){
        books = datas;
        var s = "<table cellpadding='0' cellspacing='0'" +
            " width='100%'><tr class='theader'>" +
            "<th>物料类型</th>" +
            "<th>物料名称</th><th>物料编码</th>" +
            "<th>物料SN号</th><th>供应商类型</th>" +
            "<th>供应商名称</th><th>供应商编码</th>" +
            "<th>物料仓库</th><th>成本价</th>" +
            "<th>状态</th></tr>";
        for(var i=0;i<books.length;i++){
            var b = books[i];
            s += "<tr class='list'>" +
                "<td class='ntitle'>"+b.obj_type+"</td>" +
                "<td class='ntitle'>"+b.name+"</td>" +
                "<td class='ntitle'>"+b.code+"</td>" +
                "<td class='ntitle'>"+b.code_SN+"</td>" +
                "<td class='ntitle'>"+b.admin_type+"</td>" +
                "<td class='ntitle'>"+b.admin_name+"</td>" +
                "<td class='ntitle'>"+b.admin_code+"</td>" +
                "<td class='ntitle'>"+b.obj123+"</td>" +
                "<td class='ntitle'>"+b.money+"</td>" +
                "<td class='ntitle'>"+b.status+"</td>" +
                "<td class='ntitle'><a href=''>删除</a></td>" +
                "</tr>"
        }
        s += "<table>";
        list_div.innerHTML = s;
        loading.style.display = "none";
    })
}

function add(){
    list_div.style.display = "none";
    add_div.style.display = "block";
    search_div.style.display = "none";
    return_btn.style.display = "block";
    save_btn.style.display = "block";
    add_btn.style.display = "none";
}

function returnMain(){
    list_div.style.display = "block";
    add_div.style.display = "none";
    search_div.style.display = "block";
    return_btn.style.display = "none";
    save_btn.style.display = "none";
    add_btn.style.display = "block";
}

function save(){
    if(!saving){
        saving = true;
        ajax("add_book",{
            obj_type:obj_type.value,
            name123:name123.value,
            code:code.value,
            code_SN:code_SN.value,
            admin_type:admin_type.value,
            admin_name:admin_name.value,
            admin_code:admin_code.value,
            money:money.value,
            status:status1.value,
            obj123:obj123.value,

        },function(data){
            if(data=="1"){
                alert("新增成功！");
                returnMain()
                loadData();
            }else{
                alert("新增失败！");
            }
            saving = false;
        })
    }
}